package Greed;

import java.util.Arrays;

//分发饼干
public class AssignCookies {
    public int findContentChildren(int[] g, int[] s) {
        int n=g.length;
        int m=s.length;
        Arrays.sort(g);
        Arrays.sort(s);
        int count=0;
        int right=s.length-1;
        for (int i = Math.min(m,n)-1; i >=0;i--) {
            if(s[right]>=g[i]){
                count++;
                right--;
            }
        }
        return count;
    }
    public int findContentChildren2(int[] g, int[] s) {
        int m=g.length;
        int n=s.length;
        Arrays.sort(g);
        Arrays.sort(s);
        int count=0;
        for (int i = 0,j=0; i <m&& j<n ; i++,j++) {
            while(j<n&& s[j]<g[i]) j++;
            if(j<n) count++;
        }
        return count;
    }
}
